package com.hmkx.app.dao;

import com.hmkx.app.entity.Articles;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

public interface ArticlesDao extends PagingAndSortingRepository<Articles, Long>, JpaSpecificationExecutor<Articles> {

	/**
	 * 增加文章收藏数
	 * @param articeId
	 * @return
	 */
	@Modifying
	@Query("update  Articles set collectCounts=collectCounts+1 where id=?1 ")
	int addArticleCollect(long articeId);
	/**
	 * 减少文章收藏数
	 * @param articeId
	 * @return
	 */
	@Modifying
	@Query("update  Articles set collectCounts=collectCounts-1  where id=?1 and collectCounts>0")
	int delArticleCollect(long articeId);


	//List<Articles> findByCatalogIdNotIn(String catalogId,Specification<Articles> arg0, Pageable arg1);
	/**
	 * 获取 个人的文章列表
	 * @param memCard
	 * @param start
	 * @param pageNum
	 * @return
	 */
	@Query(value="select a.* from articles a left join sys_users u on a.ee_user_id=u.user_name   where  locate(u.real_name, a.author)>0 and a.status=2 and a.deleted=0 and a.type=0  and a.ee_user_id=?1 order by a.create_time desc limit ?2,?3",nativeQuery=true)
    List<Articles> findPageListByMemCard(String memCard, int start, int pageNum);
	
	@Query(value="select a.* from articles a where a.status=2 and a.deleted=0 and a.type in (0,1) and a.mem_card=?1 and a.id<?2 order by a.create_time desc limit ?3,?4",nativeQuery=true)
    List<Articles> findPageListByArtMemCard(String memCard, long artid, int start, int pageNum);
	/**
	 * 获取 个人的文章列表
	 * @param memCard
	 * @param start
	 * @param pageNum
	 * @return
	 */
	@Query(value="select a.* from articles a left join sys_users u on a.ee_user_id=u.user_name   where  locate(u.real_name, a.author)>0 and a.status=2 and a.deleted=0 and a.type=0  and a.mem_card=?1 order by a.create_time desc limit ?2,?3",nativeQuery=true)
    List<Articles> findArticleListByMemCard(String memCard, int start, int pageSize);

    @Query(value="select a.* from articles a where a.status=2 and a.deleted=0 and a.id in (?1) ",nativeQuery=true)
    List<Articles> findarticlelistByIds(List<Integer> ids);
}
